import java.util.ArrayList;


public class Ex7_4 {

	/**
	 * PLEASANTLY
	   -ME--AN-LY
	   https://class.coursera.org/bioinformatics-001/forum/thread?thread_id=768
	 */
	public Ex7_4() {
		resultV = "";
		resultW = "";		
	}

	public String resultV, resultW;
	Integer sigma;

	
	
	
	public static void main(String[] args) {
		Ex7_4 exer = new Ex7_4();
		ArrayList<String> inputs = Utils.readLines("C:\\Users\\josemfer\\Downloads\\dataset_78_8.txt");
		String v = inputs.get(0);
		String w = inputs.get(1);
		ArrayList<Integer[][]> results = exer.getBacktrack(v, w);
		Integer[][] backt = results.get(0);
		Integer[][] scores = results.get(1);
		Integer finalScore = scores[v.length()][w.length()];
		exer.getOutputLCS(backt,v,w,v.length(),w.length());
		Utils.writeFile( finalScore + "\r\n" 
				+ exer.resultV + "\r\n" 
				+ exer.resultW, 
				"C:\\Users\\josemfer\\Downloads\\out_7_4.txt");
		System.out.println(finalScore);
		System.out.println(exer.resultV);
		System.out.println(exer.resultW);
		//System.out.println("");
		//utils.printMatrix(scores,v,w);

	}


	private void getOutputLCS(Integer[][] backtrack, 
			String v, String w, Integer i, Integer j) {
		//System.out.println("i=" + i + "   j=" + j);
		if (i == 0 || j == 0) {
			return;
		}
		if (backtrack[i][j] == 0) { //  down
			getOutputLCS(backtrack,v,w,i-1,j);			
			resultV += v.charAt(i-1);
			resultW += "-";

		} else if (backtrack[i][j] == 1) {   //  right 
			getOutputLCS(backtrack,v,w,i,j-1);
							
			resultV += "-";
			resultW += w.charAt(j-1);
			
			//System.out.print(w.charAt(j-1));

		} else if (backtrack[i][j] == 2) {
			getOutputLCS(backtrack,v,w,i-1,j-1);   //  diagonal
			resultV += v.charAt(i-1);
			resultW += w.charAt(j-1);			
		}		
	}	

	/*
	 *   '1' = down   '0' = right   '2' = diagonal
	 */
	private ArrayList<Integer[][]> getBacktrack(String v, String w) {	
		ArrayList<Integer[][]> results = null;			
		Integer sigma = 1;
		Integer gap = 11;
		String blosum62 = "ACDEFGHIKLMNPQRSTVWY";		
		Integer[][] blosumMatrix = 
			 Utils.readMatrix("C:\\Users\\josemfer\\Downloads\\BLOSUM62.txt");
		Integer n = v.length();
		Integer m = w.length();
		Integer s[][] = new Integer[n+1][m+1];
		Integer back[][] = new Integer[n+1][m+1];
		
		for (int i=0;i<=n;i++) {
			s[i][0] = 0;
			back[i][0] = 0; // down
		}
		for (int j=0;j<=m;j++) {
			s[0][j] = 0;
			back[0][j] = 1; // right
		}
		for (int i=1;i<=n;i++) {
			for (int j=1;j<=m;j++) {
				// calculate down and right
				Integer down = s[i-1][j] - gap;
				
				
				if (i==24 && j==15) {
					System.out.println();
				} 
				
				try {
					if (back[i-1][j] == 0   ) {
						down = s[i-1][j] - sigma;
					}
				} catch(Exception e){}				
				
				Integer right = s[i][j-1] - gap;				
				try {
					if (back[i][j-1] == 1 ) {
						right = s[i][j-1] - sigma ;	
					}
				} catch(Exception e){}
				
				
				
				// calculate diag				
				char vi = v.charAt(i-1);
				char wj = w.charAt(j-1);
				Integer blosum_i = blosum62.indexOf(vi);
				Integer blosum_j = blosum62.indexOf(wj);
				Integer diag = s[i-1][j-1] + blosumMatrix[blosum_i][blosum_j];
				//save score and backtrack matrix
				s[i][j] = Utils.max(new Integer[]{down,right,diag});			 
				back[i][j] = Utils.maxIndex(new Integer[]{down,right,diag});			
			}				
		}
		results = new ArrayList<Integer[][]>();		
		results.add(back);		
		results.add(s);		
		return results;
	}

}
